//
//  ROShopList.swift
//  NeusoftSchool
//
//  Created by 李狗蛋 on 2017/4/1.
//  Copyright © 2017年 李狗蛋. All rights reserved.
//

import UIKit

class ROShopList: UITableViewController {
    
    var shopList: NSArray = NSArray()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        RONetworkMngTool.sharedNetwodkMngTool().RONetwork_GetShopListView(self.view) { (shopList: NSArray) in
            self.shopList = shopList
            self.tableView.reloadData()
        }
    
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }


    override func numberOfSections(in tableView: UITableView) -> Int {

        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if self.shopList.count == 0 {
            return 0
        }
        return self.shopList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "shopCell", for: indexPath)
        if self.shopList.count != 0 {
            let shop: ROShopModel = self.shopList[(indexPath as NSIndexPath).row] as! ROShopModel
            cell.textLabel!.text = shop.shopname as String
            let imageStr = shop.pic as String
            let imageUrl = URL(string: imageStr)
            cell.imageView!.setImageWith(imageUrl, placeholderImage: UIImage(named: "imageViewHolder"))
            cell.accessoryType = UITableViewCellAccessoryType.disclosureIndicator
        }
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let currentShop: ROShopModel = shopList[(indexPath as NSIndexPath).row] as! ROShopModel
        self.performSegue(withIdentifier: "toFoodList", sender: currentShop)
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        
        //let foodListController = segue.destination as! ROFoodList
        let foodListController: ROFoodList = segue.destination as! ROFoodList
        foodListController.shop = sender as! ROShopModel
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 80
    }
    

}
